package com.chinacoal.microservice.impl.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chinacoal.microservice.api.model.entity.UtaskAttrEntity;
import com.chinacoal.microservice.impl.mapper.UtaskAttrMapper;
import com.chinacoal.microservice.impl.service.UtaskAttrService;
import com.chinacoal.microservice.impl.service.UtaskCatAttrMapService;
import com.chinacoal.microservice.impl.util.UtaskCommonFun;
import com.chinacoal.microservice.model.syscenter.ComboxEntity;
import com.chinacoal.microservice.util.PrimaryKey;

@Service
public class UtaskAttrServiceImpl extends ServiceImpl<UtaskAttrMapper, UtaskAttrEntity> implements UtaskAttrService {

    @Resource
    private UtaskAttrMapper utaskAttrMapper;
    @Resource
    private UtaskCatAttrMapService utaskCatAttrMapService;

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void addRegAttr(UtaskAttrEntity utaskAttrEntity) throws Exception {

		// 校验
		String attrCode = utaskAttrEntity.getAttrCode();
		Integer count = utaskAttrMapper.getTaskAttrCount(attrCode);
		if(count != null && count > 0) throw new Exception("该待办类型扩展属性已存在，请勿重复添加");
		
		// 执行新增 
		utaskAttrEntity.setAttrId(PrimaryKey.newKey());
		utaskAttrMapper.insert(utaskAttrEntity);
	}

	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void updateRegAttr(UtaskAttrEntity utaskAttrEntity) throws Exception {
		
		// 执行修改
		utaskAttrMapper.updateById(utaskAttrEntity);
		
		// 校验
		String attrCode = utaskAttrEntity.getAttrCode();
		Integer count = utaskAttrMapper.getTaskAttrCount(attrCode);
		if(count != null && count > 1) throw new Exception("该待办类型扩展属性已存在，请检查");
	}

	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void deleteRegAttr(String attrId) throws Exception {
		
		// 校验
		Integer count = utaskCatAttrMapService.getCatAttrMapCount(null, null, attrId);
		if(count != null && count > 0) throw new Exception("该待办类型扩展属性已被使用，请勿删除");
	
		// 执行删除
		utaskAttrMapper.deleteById(attrId);
	}

	@Override
	public Page<UtaskAttrEntity> getList(Integer page, Integer pageSize, String expression) throws Exception{
		
    	Page<UtaskAttrEntity> utaskAttrPage = UtaskCommonFun.getPage(page, pageSize);
		
		List<UtaskAttrEntity> UtaskAttrList = utaskAttrMapper.findList(utaskAttrPage, expression);
		
		return utaskAttrPage.setRecords(UtaskAttrList);
	}

	@Override
	public List<ComboxEntity> getTaskAttrComboBox() throws Exception{
		// TODO Auto-generated method stub
		return utaskAttrMapper.getTaskAttrComboBox();
	}

}
